//
//  MSLeadViewController.m
//  SocietySale
//
//  Created by Mike on 16/10/31.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSLeadViewController.h"
#import "MSLoginViewController.h"
#import "MSRegistViewController.h"

@interface MSLeadViewController ()
{
    NSMutableArray *imageArray;
}
@property (nonatomic, strong) UIScrollView *scrollView;
//图片浏览
@property(nonatomic,strong)HDImagesSkimView *imgsSkimView;
@property (nonatomic, strong) UIPageControl *pageCtr;
@end

@implementation MSLeadViewController
@synthesize pageCtr;

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // 禁用返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)initUI{
    NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
    imageArray = [NSMutableArray arrayWithObjects:@"startImage.jpg", @"2", @"4.jpg", @"3.jpg", nil];
    for (NSInteger i=0; i<imageArray.count; i++){
        propertyPicture* item = [[propertyPicture alloc] init];
        item.path = imageArray[i];
        [dataArray addObject:item];
    }
    
    self.imgsSkimView = [[HDImagesSkimView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-60)];
    self.imgsSkimView.disableTimer = YES;
    self.imgsSkimView.clipsToBounds = YES;
    self.imgsSkimView.disableTimer = NO;
//    self.imgsSkimView.dataArray = dataArray;
    [self.view addSubview:self.imgsSkimView];
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
    NSString *imageFile = [documentsPath stringByAppendingPathComponent:@"StartImageArrayFile.plist"];
    
    if ([fileManager fileExistsAtPath:imageFile]) {
        NSArray *tempArray = [[NSMutableArray alloc] initWithContentsOfFile:imageFile];
        self.imgsSkimView.dataArray = tempArray;
        if (tempArray.count<1) {
            propertyPicture* item = [[propertyPicture alloc] init];
            item.path = @"startImage.jpg";
            self.imgsSkimView.dataArray = @[item];
        }
    }
    
    GXButton *joinBtn = [self creatBtnWithTitle:@"立即加入" Frame:CGRectMake(0, SCREEN_HEIGHT-60, SCREEN_WIDTH/2, 60)];
    joinBtn.tag = 10;
    [joinBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    joinBtn.backgroundColor = kBlueColor;
    [joinBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    GXButton *loginBtn = [self creatBtnWithTitle:@"登录" Frame:CGRectMake(SCREEN_WIDTH/2, SCREEN_HEIGHT-60, SCREEN_WIDTH/2, 60)];
    loginBtn.tag = 20;
    loginBtn.backgroundColor = [UIColor whiteColor];
    [loginBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)testDataFunc
{
    [netEngine requestStartImageWithTarget:self.VctrID hudView:nil];
}

- (void)updateViewController:(NSNotification *)note
{
    NSDictionary *responseDict = note.userInfo;
    if (responseDict[Receiver] == self.VctrID) {
        if (responseDict[@"StartImageArray"] != nil){
            NSMutableArray *resultArray = responseDict[@"StartImageArray"];
            self.imgsSkimView.dataArray = resultArray;
            if (resultArray.count<1) {
                propertyPicture* item = [[propertyPicture alloc] init];
                item.path = @"startImage.jpg";
                self.imgsSkimView.dataArray = @[item];
            }
        }
    }
}

- (GXButton*)creatBtnWithTitle:(NSString*)title Frame:(CGRect)frame{
    GXButton *btn = [GXButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = HeitiFont(16);
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    return btn;
}

- (void)launchBtnClick:(GXButton *)button {
//    [rootNavigation popViewControllerAnimated:YES];
}

- (void)btnClick:(GXButton *)btn {
    if (btn.tag == 10) {
        MSRegistViewController *vc = [[MSRegistViewController alloc] init];
        vc.registType = MSRegistType_Regist;
        [rootNavigation pushViewController:vc animated:YES];
    }
    if (btn.tag == 20) {
        MSLoginViewController *vc = [[MSLoginViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    pageCtr.currentPage = scrollView.contentOffset.x/SCREEN_WIDTH;
}

@end
